#include <xpam/system/pipe.h>
#include <xpam/exception.h>

namespace xpam
{
	namespace system
	{
		void pipe::create(handle & rd, handle & wr, int sz)
		{
			void * hr, * hw;

			SECURITY_ATTRIBUTES attr; 
			attr.nLength = sizeof(SECURITY_ATTRIBUTES); 
			attr.bInheritHandle = TRUE;
			attr.lpSecurityDescriptor = NULL; 

			if (!CreatePipe(&hr, &hw, &attr, sz))
				throw xpam::exception() << "pipe is not created";

            rd.internal_handle(hr);
			wr.internal_handle(hw);
		}

		//    std::stringstream log;
		//#define LOG {std::cerr
		//#define ENDLOG std::endl;}

		//pipe::pipe()
		//{
		//}

		//void pipe::create(const std::string & name, const int buffSize)
		//{
		//    std::string pipename = std::string("\\\\.\\pipe\\") + name;

		//    void * h = CreateNamedPipeA(pipename.c_str(),
		//        PIPE_ACCESS_DUPLEX,
		//        PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
		//        PIPE_UNLIMITED_INSTANCES,
		//        buffSize, buffSize,
		//        NMPWAIT_USE_DEFAULT_WAIT, NULL);
		//    set_handle(h);
		//    LOG << "pipe::create::CreateNamedPipeA() = " << hex << h << ENDLOG;
		//    if (h == INVALID_HANDLE_VALUE) 
		//    {
		//        _pipe = 0;
		//        return;
		//    }

		//    BOOL connected = ConnectNamedPipe(_pipe, NULL);
		//    LOG << "pipe::create::ConnectNamedPipe() = " << connected << ENDLOG;
		//    if (!connected)
		//    {
		//        CloseHandle(_pipe); 
		//        _pipe = 0;
		//        return;
		//    }
		//}

		//void pipe::connect(const std::string & name)
		//{
		//    string pipename = string("\\\\.\\pipe\\") + name;
		//    _pipe = CreateFileA(pipename.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);          // no template file 
		//    LOG << "pipe::connect::CreateFileA() = " << hex << _pipe << ENDLOG;
		//    if (_pipe == INVALID_HANDLE_VALUE) 
		//    {
		//        _pipe = 0;
		//        return;
		//    }
		//    //BOOL wait = WaitNamedPipeA(pipename.c_str(), 10000);
		//    //LOG << "pipe::connect::WaitNamedPipeA() = " << dec << wait << ENDLOG;
		//}

		//void pipe::read(std::string & message, const int buffSize)
		//{
		//    DWORD cbBytesRead = 0;
		//    message.resize(buffSize);
		//    BOOL fSuccess = ReadFile(_pipe, (LPVOID)message.c_str(), (DWORD)message.size(), &cbBytesRead, NULL);
		//    LOG << "pipe::read::ReadFile() = " << dec << fSuccess << ", " << cbBytesRead << ENDLOG;
		//    message.resize(cbBytesRead);
		//    if (!fSuccess) 
		//    {
		//        CloseHandle(_pipe);
		//        _pipe = 0;
		//        return; 
		//    }

		//}

		//void pipe::write(const std::string & message)
		//{
		//    DWORD cbWritten = 0;
		//    BOOL fSuccess = WriteFile(_pipe, (LPVOID)message.c_str(), (DWORD)message.size(), &cbWritten, NULL);
		//    LOG << "WriteFile() = " << dec << fSuccess << ", " << cbWritten << ENDLOG;
		//    if (!fSuccess)
		//    {
		//        CloseHandle(_pipe);
		//        _pipe = 0;
		//        return;
		//    }
		//}
	}
}